<template>
    <div class="crm-crmCustomer-container">
        <el-dialog title="新增客户" v-model="isShowDialog" :close-on-click-modal="false">
            <el-form ref="addCrmCustomerFormRef" :model="formData" :rules="formRules" label-width="95px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="客户名称" prop="customerName">
                            <el-input v-model="formData.customerName" placeholder="请输入客户名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="客户级别" prop="level">
                            <Dictionary v-model="formData.level" dict-label="crm_customer_level" placeholder="请选择客户级别"></Dictionary>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="手机号码" prop="mobile">
                            <el-input v-model="formData.mobile" placeholder="请输入手机号码" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="客户来源" prop="source">
                            <Dictionary v-model="formData.source" dict-label="crm_customer_source" placeholder="请选择客户来源"></Dictionary>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="身份证号码" prop="idNumber">
                            <el-input v-model="formData.idNumber" placeholder="请输入身份证号码" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="电话号码" prop="telephone">
                            <el-input v-model="formData.telephone" placeholder="请输入电话号码" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="邮箱号码" prop="email">
                            <el-input v-model="formData.email" placeholder="请输入邮箱号码" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="关联企业" prop="enterpriseIds">
                            <el-select v-model="formData.enterpriseIds" multiple filterable remote 
                                @click="loadEnterpriseData()"
                                :remote-method="loadEnterpriseData" placeholder="请选择关联企业" clearable class="w100">
								<el-option 
									v-for="item in enterprises"
									:key="item.id"
									:label="item.enterpriseName"
									:value="item.id">
								</el-option>
							</el-select>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="备注" prop="remark">
                            <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel">取 消</el-button>
                    <el-button type="primary" @click="onSubmit">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { addCrmCustomer } from '/@/api/crm/customer/index';
import { getCrmEnterpriseList } from '/@/api/crm/enterprise/index';
import Dictionary from '/@/components/dictionary/index.vue';
export default {
    name: 'crm/customer/add',
    components: { Dictionary },
    setup(props, context) {
        const addCrmCustomerFormRef = ref();
        const state = reactive({
            isShowCrmEnterpriseDialog: false,
            isShowDialog: false,
            enterprises: [],
            formData: {
            },
            formRules : {
                customerName: { required: true, message: '客户名称不能为空', trigger: 'blur' },
                level: { required: true, message: '客户级别不能为空', trigger: 'blur' },
                // idNumber: { required: true, message: '身份证号码不能为空', trigger: 'blur' },
                source: { required: true, message: '客户来源不能为空', trigger: 'blur' },
                mobile: { required: true, message: '手机号码不能为空', trigger: 'blur' },
            }
        });

        const loadEnterpriseData = (query?: string) => {
            if(query == '') return;
            getCrmEnterpriseList({enterpriseName: query}).then((res) => {
                if(res.code == 0){
                    state.enterprises = res.data;
                }
            });
        }
        
        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            addCrmCustomerFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = () => {
            addCrmCustomerFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        customerName: state.formData.customerName,
                        level: state.formData.level,
                        source: state.formData.source,
                        idNumber: state.formData.idNumber,
                        mobile: state.formData.mobile,
                        telephone: state.formData.telephone,
                        email: state.formData.email,
                        enterpriseIds: state.formData.enterpriseIds,
                        remark: state.formData.remark,
                    }
                    addCrmCustomer(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('保存成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        return {
            addCrmCustomerFormRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            loadEnterpriseData,
            ...toRefs(state),
        };
    },
};
</script>